﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Pickers
{
    /// <summary>
    /// 返回类型：WebRef.VMaterial
    /// </summary>
    public partial class MaterialPicker : UserControl, IView
    {
        #region fileds
        private int _pageIndex;
        private int _pageCount;
        private int _status;
        Promise _promise = new Promise();
        #endregion

        public MaterialPicker(int status)
        {
            InitializeComponent();
            _status = status;
            ctlGrid.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }
        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("ok", Resources.isOK, "确定", OnOKActionClick))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", OnRefreshActionClick))
                .AddAction(new ActionNode("cancel", Resources.isDelete, "取消", OnCancelActionClick));
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                SearchLookUpEditUtil.BindEMasterDataItem(this.ctlSearchLookUpMaterialCatagory, _promise,
                    Domain.Constant.MasterDataCodes.MaterialCategory, null, null, false, true);
            })
            .Then(LoadMaterialListData)
            .Run();
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("选择商品配件");
        }

        #endregion

        #region actions
        private void OnOKActionClick(object obj1, object obj2)
        {
            WebRef.VMaterial selMaterial = Common.GridControlUtil.GetSelectObject<WebRef.VMaterial>(ctlGrid);
            if (selMaterial != null)
            {
                UIContext.Current.ViewManager.CloseView(true, selMaterial);
            }
        }

        private void OnCancelActionClick(object obj1, object obj2)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        private void OnRefreshActionClick(object obj1, object obj2)
        {
            _pageIndex = 0;
            LoadMaterialListData();
        }
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            LoadMaterialListData();
        }
        /// <summary>
        /// 双击行选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlGridView_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2)
            {
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hInfo = ctlGridView.CalcHitInfo(new Point(e.X, e.Y));
                if (hInfo.InRow)
                {
                    if (ctlGridView.FocusedRowHandle > -1)
                    {
                        WebRef.VMaterial vMaterial = ctlGridView.GetFocusedRow() as WebRef.VMaterial;
                        UIContext.Current.ViewManager.CloseView(true, vMaterial);
                    }
                }
            }
        }
        #endregion

        #region methods
        private void LoadMaterialListData()
        {
            this.ShowMask();
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VMaterial> result = new Service.QueryService().QueryMaterialPagedList(
                    this.ctlEditQry.Text.Trim(),
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    GetSelectedCatgoryCode(),
                    null,
                    _status,
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.ctlGrid.Paginate<WebRef.VMaterial>(result, out _pageIndex, out _pageCount);
                        _promise.Reject();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        private int GetPageLimit()
        {
            return 25;
        }
        private string GetSelectedCatgoryCode()
        {
            WebRef.EMasterDataItem selItem = SearchLookUpEditUtil.GetSelectedValue<WebRef.EMasterDataItem>(this.ctlSearchLookUpMaterialCatagory);
            if (selItem == null)
            {
                return null;
            }
            return selItem.Code;
        }
        #endregion

    }
}
